<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\api\controller\shop;

use app\api\controller\shop\Admin;
use think\Db;

/**
 * @title 【会员】
 */
class Member extends Admin {

    private $user_fields = 'u.id,u.avatar,u.nickname,u.openid,u.vip,u.points,u.group_id';

    /**
     * @title 当前用户的积分获取列表
     */
    public function points() {




        if (UID) {

            $lists = db('member_points')->where('member_id', UID)->order('id desc')->select();

            foreach ($lists as $key => $value) {
                $lists[$key]['create_time'] = date('Y/m/d H:i:s', $value['create_time']);
            }

            $lists = array_merge($lists);

            return ['code' => 0, 'msg' => 'success', 'data' => $lists];
        } else {
            return ['code' => 1, 'msg' => 'params missing2'];
        }
    }

    /**
     * @title 当前用户的积分获取列表
     */
    public function points_sum() {


        if (UID) {


            $points = db('member_points')->where('member_id', UID)->sum('points');


            return ['code' => 0, 'msg' => 'success', 'data' => $points];
        } else {
            return ['code' => 1, 'msg' => 'params missing2'];
        }
    }

    /**
     * @title 用户详情
     * @method post
     */
    public function index() {


        header("Access-Control-Allow-Origin: *");
        header('Access-Control-Allow-Headers:x-requested-with,content-type,tk-uid,tk-token');


        $member = db('member')->alias('u')->where('id', UID)->field($this->user_fields)->find();

        
        //vipinfo
//        if ($member['vip'] > 0 && $member['vip'] > time()) {
//            $member['vipinfo'] = '会员到期时间：' . date('Y年m月d日', $member['vip']);
//        } else {
//            $member['vipinfo'] = '你当前还不是VIP会员';
//        }
        
        // 用户头衔
        $group = db('member_group')->where('id', $member['group_id'])->field('*')->find();
        $group['icon'] = $group['icon'] ? APP_HOST.APP_URL.res_http($group['icon']) : '';
        
        $member['group'] = $group;
        
        

        $daifukuan = db('order')->where('member_id', UID)->where('delete_time', 'null')->where('status', 10)->count();
        $daifahuo = db('order')->where('member_id', UID)->where('delete_time', 'null')->where('status', 20)->count();
        $daishouhuo = db('order')->where('member_id', UID)->where('delete_time', 'null')->where('status', 30)->count();

        // 待评价    
        $daipingjia = Db::name('order_goods')
                ->where('order_id', 'IN', function($query) {
                    $query->name('order')->where('member_id', UID)->where('status', 40)->field('id');
                })
                ->where('has_evaluate', 0)
                ->count();
                
        // 待售后处理
        $daichuli = db('order_service')->where('member_id', UID)->where('status',1)->count();


        $statics = [
            'daifukuan' => $daifukuan,
            'daifahuo' => $daifahuo,
            'daishouhuo' => $daishouhuo,
            'daipingjia' => $daipingjia,
            'daichuli' => $daichuli,
        ];


        return ['code' => 0, 'msg' => 'success', 'data' => compact('member', 'statics')];
    }

}
